#include"protocol.hpp"
#include"socket.hpp"


int main(int argc,char** argv)
{
    if(argc != 3)
    {
        std::cerr <<"Usage:" << argv[0] << " serverip serverport"<<std::endl;
        std::exit(static_cast<int>(ExitCode::USAGE_ERR));
    }
    TcpSocket tcpsocket;
    tcpsocket.BindTcpClientSocket();
    tcpsocket.SocketConnect(argv[1],std::stoi(argv[2]));
    Protocol protocol;
    while(true)
    {
        int x,y;
        char oper;
        std::cout << "please input x y oper:";
        std::cin >> x >> y >>oper;
        Request request(x,y,oper);
        std::string jsonstr =  request.Serialize();
        std::string wstr = protocol.Encode(jsonstr);
        tcpsocket.Write(wstr);
        std::string buffer_queue;
        while(true)
        {
            tcpsocket.Read(buffer_queue);
            std::string jsonstr;
            if(protocol.Decode(buffer_queue,jsonstr))
            {
                Response response;
                response.Deserialize(jsonstr);
                response.Show();
                break;
            }
        }
    }   

    
    return 0;
}